package view.timeline;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JSlider;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import constants.Icons;
import controller.CompleteListener;



import view.BaseComponent;

public class TimelinePanel extends BaseComponent implements ChangeListener, ActionListener {

	private JSlider timeline;
	private JTextArea currentTimeArea;
	private JTextField maxMinField;
	private JTextField maxSecField;
	private JButton update;
	private JButton leftFrame;
	private JButton rightFrame;
	
	public TimelinePanel() {
		timeline = new JSlider(SwingConstants.HORIZONTAL,0,10500,0);
		currentTimeArea = new JTextArea("0:0");
		maxMinField = new JTextField("1");
		maxSecField = new JTextField("45");
		update = new JButton("Update");
		
		currentTimeArea.setEditable(false);
		
		update.setActionCommand("update");
		update.setToolTipText("Updates the round time");
		update.addActionListener(this);
		
		leftFrame = new JButton(Icons.getIcon("leftFrame"));
		
		leftFrame.setActionCommand("leftFrame");
		leftFrame.setToolTipText("Decrements one frame.");
		leftFrame.addActionListener(this);
		
		rightFrame = new JButton(Icons.getIcon("rightFrame"));
		
		rightFrame.setActionCommand("rightFrame");
		rightFrame.setToolTipText("Increments one frame.");
		rightFrame.addActionListener(this);
		
		timeline.setMajorTickSpacing(500);
		timeline.setMinorTickSpacing(100);
		timeline.setPaintTicks(true);
		timeline.setPaintLabels(false);
		
		timeline.addChangeListener(this);
		
		this.setLayout(new BoxLayout(this,BoxLayout.LINE_AXIS));
		this.setBorder(BorderFactory.createEmptyBorder(3,3,3,3));
		
		this.add(currentTimeArea);
		this.add(Box.createHorizontalStrut(5));
		this.add(leftFrame);
		this.add(Box.createHorizontalStrut(3));
		this.add(timeline);
		this.add(Box.createHorizontalStrut(3));
		this.add(rightFrame);
		this.add(Box.createHorizontalStrut(5));
		this.add(maxMinField);
		this.add(new JLabel(" : "));
		this.add(maxSecField);
		this.add(Box.createHorizontalStrut(5));
		this.add(update);
		
		currentTimeArea.setPreferredSize(new Dimension(40,20));
		currentTimeArea.setMaximumSize(new Dimension(40,20));
		maxMinField.setMaximumSize(new Dimension(40,20));
		maxSecField.setMaximumSize(new Dimension(40,20));
		maxMinField.setPreferredSize(new Dimension(20,20));
		maxSecField.setPreferredSize(new Dimension(25,20));
		
		update.setMaximumSize(new Dimension(60,20));
		leftFrame.setMaximumSize(new Dimension(Icons.getIcon("leftFrame").getIconWidth()+4,
				Icons.getIcon("leftFrame").getIconHeight()+4));
		leftFrame.setPreferredSize(new Dimension(Icons.getIcon("leftFrame").getIconWidth()+4,
				Icons.getIcon("leftFrame").getIconHeight()+4));
		rightFrame.setMaximumSize(new Dimension(Icons.getIcon("rightFrame").getIconWidth()+4,
				Icons.getIcon("rightFrame").getIconHeight()+4));
		rightFrame.setPreferredSize(new Dimension(Icons.getIcon("rightFrame").getIconWidth()+4,
				Icons.getIcon("rightFrame").getIconHeight()+4));
		
		this.setMaximumSize(new Dimension(9999,40));
	}
	
	public int getEndTime() {
		
		return 60*Integer.parseInt(maxMinField.getText())
		+Integer.parseInt(maxSecField.getText());
	}
	
	public double getCurrentTime() {
		return (double)timeline.getValue()/100;
	}
	
	public void setCurrentTime(double time) {
		timeline.setValue((int)(time*100));
	}

	public void setUpdateEnabled(boolean enable) {
		maxMinField.setEnabled(enable);
		maxSecField.setEnabled(enable);
		update.setEnabled(enable);
	}
	
	@Override
	public void attachListener(CompleteListener l) {
		timeline.addChangeListener(l);
		update.addActionListener(l);
	}

	@Override
	public void setEnabled(boolean enable) {
		timeline.setEnabled(enable);
		maxMinField.setEnabled(enable);
		maxSecField.setEnabled(enable);
		update.setEnabled(enable);
	}

	public void setEditable(boolean editable) {
		maxMinField.setEnabled(editable);
		maxSecField.setEnabled(editable);
		update.setEnabled(editable);
	}
	
	public void stateChanged(ChangeEvent e) {
		int val = timeline.getValue()/100;
		currentTimeArea.setText(val/60+":"+(val-60*(val/60)));
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("update"))
			timeline.setMaximum(getEndTime()*100);
		if (e.getActionCommand().equals("leftFrame"))
			timeline.setValue(timeline.getValue()-10);
		if (e.getActionCommand().equals("rightFrame"))
			timeline.setValue(timeline.getValue()+10);
	}
}
